package com.doge.controller.admin;

import com.doge.bean.base.BaseData;
import com.doge.bean.base.BaseParam;
import com.doge.bean.base.BaseRespVo;
import com.doge.bean.pojo.storage.Storage;
import com.doge.exception.Param402Exception;
import com.doge.service.admin.storage.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @program: doge
 * @description:
 * @author: Keyu Li
 * @create: 2021-11-29 20:53
 **/

@RestController
@RequestMapping({"admin/storage","wx/storage"})
public class StorageController {

    @Autowired
    StorageService storageService;

    @RequestMapping("list")
    public BaseRespVo list(BaseParam param, String key, String name){
        BaseData data = storageService.list(param, key, name);
        return BaseRespVo.ok(data);
    }

    @PostMapping ({"create","upload"})
    public BaseRespVo create(@RequestParam("file") MultipartFile file) throws Exception {
        // 先做一下基本判断
        if (file.isEmpty()){
            throw new Param402Exception("文件为空！");
        }
        // 图片上传，存到 storage表
        Storage storage = storageService.create(file);
        return BaseRespVo.ok(storage);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody Storage storage){
        storageService.update(storage);
        return BaseRespVo.ok(storage);
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Storage storage) throws Exception {
        storageService.delete(storage);
        return BaseRespVo.ok();
    }
}
